//
//  WJPublishAudioCell.m
//  WJBSApp
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJPublishAudioCell.h"
#import <AVFoundation/AVFoundation.h>

@interface WJPublishAudioCell ()

@property (nonatomic, retain) WJBaseView *belowView;

@property (nonatomic, retain) WJBaseLable *textLab;

@property (nonatomic, retain) WJHotLable *timeLable;

@property (nonatomic, retain) WJBaseButton *deleteButton;

@property (nonatomic, retain) WJEssAndNewButton *playButton;

@property (nonatomic, retain) UILabel *nowTime;

@property (nonatomic, retain) UILabel *totleTime;
 /** 播放*/
@property (nonatomic,retain) AVAudioPlayer *player;
 /** 定时器 */
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation WJPublishAudioCell
-(void)dealloc
{
    [_model release];
    [_belowView release];
    [_textLab release];
    [_timeLable release];
    [_deleteButton release];
    Block_release(self.Block);
    [super dealloc];
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.userInteractionEnabled = YES;
        
        self.belowView = [[WJBaseView alloc]init];
        [self.contentView addSubview:self.belowView];
        [_belowView release];
        
        self.timeLable = [[WJHotLable alloc]init];
        [self.belowView addSubview:self.timeLable];
        self.timeLable.font = [UIFont systemFontOfSize:13];
        [_timeLable release];
        
        self.deleteButton = [WJBaseButton buttonWithType:UIButtonTypeCustom];
        [self.deleteButton setImage:[UIImage imageNamed:@"Delete.png"] forState:0];
        [self.deleteButton addTarget:self action:@selector(deleteAct:) forControlEvents:1<<6];
        [self.belowView addSubview:self.deleteButton];
        
        self.textLab = [[WJBaseLable alloc]init];
        self.textLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.textLab];
        [_textLab release];
        
        self.playButton = [WJEssAndNewButton buttonWithType:UIButtonTypeCustom];
        [self.playButton setImage:[UIImage imageNamed:@"publish-audio@3x副本"] forState:0];
        [self.playButton addTarget:self action:@selector(playBut:) forControlEvents:1<<6];
        [self.contentView addSubview:self.playButton];
        
        self.nowTime = [[UILabel alloc]init];
        self.nowTime.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.nowTime];
        self.nowTime.text = @"00:00";
        [_nowTime release];

        
        self.totleTime = [[UILabel alloc]init];
        self.totleTime.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.totleTime];
        self.totleTime.textAlignment = 2;
        [_totleTime release];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.belowView.frame = CGRectMake(0, 10, WIDTH, 40);
    
    self.timeLable.frame = CGRectMake(10, 10, 150 , 30);
    
    self.deleteButton.frame = CGRectMake(WIDTH - 60, 5, 30, 30);
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGRect rect = [self.textLab.text boundingRectWithSize:CGSizeMake(WIDTH - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    self.textLab.frame = CGRectMake(10, 50, WIDTH - 20, rect.size.height);
    
    self.playButton.frame = CGRectMake(WIDTH/2-30, 60 + rect.size.height, 60, 60);
    
    self.nowTime.frame = CGRectMake(10, 75 + rect.size.height, 100, 30);
    
    self.totleTime.frame = CGRectMake(WIDTH - 110, 75 + rect.size.height, 100, 30);
    
}

- (void)setModel:(WJPublishModel *)model
{
    if (_model !=model) {
        [_model release];
        _model = [model retain];
    }
    
    self.textLab.text = model.text;
    
    self.timeLable.text = model.nowTime;
    
    self.totleTime.text = model.totalTime;
}

- (void)deleteAct:(WJBaseButton *)button
{
    NSLog(@"点击删除");
    if (self.Block) {
        self.Block(self.model.nowTime);
    }
    
}


- (void)playBut:(WJEssAndNewButton *)button
{
    /** button.selected 默认是NO*/
    if (!button.selected) {
        NSString* path = self.model.audioPath;
        NSURL* url = [NSURL fileURLWithPath:path];
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [_player prepareToPlay];
        [_player play];
        [self.playButton setImage:[UIImage imageNamed:@"publish-audio@3x副本"] forState:0];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

        
    }else{
        
        [self.playButton setImage:[UIImage imageNamed:@"音乐暂停"] forState:0];
        [_player pause];
    }
    button.selected = !button.selected;
}

- (void)levelTimer:(NSTimer *)time
{
    // 后面stringFormatWithTimeInterval是自己写的方法用于转换时间格式
    self.nowTime.text = [self withTime:[NSString stringWithFormat:@"%f",_player.currentTime]];
    
}

- (NSString *)withTime:(NSString *)time
{
    // 秒
    NSInteger sec = time.integerValue % 60;
    
    // 分
    NSInteger min = time.integerValue / 60;
    
    return [NSString stringWithFormat:@"%02zd:%02zd",min,sec];
}









@end
